/* Name: HelpScreen
 * Author: Cameron Hoerig
 * Date: 5-13-12
 * Description: The HelpScreen class displays the help text to the user
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

public class HelpScreen extends Item {
	BackBtn backBtn; // button to exit the help screen
	// string of text read from the text file
	private static LinkedList<String> helpText = new LinkedList<String>();
	
	public HelpScreen() {
		Game.create((Item)this, 1); // Needed for update, draw, and destroy
		colType = COL_NONE; // Needed for collision checking		
		readFile(); // read in the contents of the help text file
		backBtn = new BackBtn(Game.screenWidth/2,Game.screenHeight-80, 150, 50); // set the placement of the back button
	}
	
	// Keep the position updated on redraws in case screen is resized
	public void update() {
		backBtn.updateLocation(Game.screenWidth/2,Game.screenHeight-80);
	}
	
	public void leftMousePress(){}
	public void rightMousePress(){}
	public void leftMouseRelease(){}
	public void rightMouseRelease(){}
	
	// Called every time a new frame is drawn - displays help text, and calculates position for it
	public void draw() { // draw the text on screen
		Graphics2D gh = Game.getGraphicsHandle();
		gh.setFont(new Font("Serif", Font.PLAIN, 20));
		gh.setColor(Color.white);
		int curY = 50; // Set starting point for text
		for(int count=0; count < helpText.size(); count++){ // read in once line at a time
			curY = drawString(helpText.get(count), Game.screenWidth, gh, 10, curY);
			// Add space between paragraphs
			curY += 20;
			//gh.drawString(helpText.get(count), 5, 17*(count+3));
		}
	}
	
	// Draws the given string, starting a newline when it exceeds the maxWidth
	//  Returns ending y point of text
	private int drawString(String text, int maxWidth, Graphics2D gh, int x, int y) {
		FontMetrics fm = gh.getFontMetrics();
		String[] splitText = text.split(" ");
		int textHeight = fm.getHeight()-5; // Too much space, so decrease 5
		int curX = x;
		int curY = y;
		for (String eachWord:splitText) {
			int curWordWidth = fm.stringWidth(eachWord);
			
			// Bump to new line if exceeds space - leave gap of 5 pixels on right
			if (curX + curWordWidth >= maxWidth + x - 10) {
				// Bump to next line and reset x
				curY += textHeight;
				curX = x;
			}
			gh.drawString(eachWord, curX, curY);
			curX+=curWordWidth + 5;
		}
		return curY;
	}
	
	// Reads a file in, and places into linked list
	private static void readFile() {
		helpText.clear();
		FileInputStream fstream = null;
		try {// try to open the help text file
			fstream = new FileInputStream("HelpDocument.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    try {// read in each line and add each one as a node on the linked list
	    	String tempStr = br.readLine();
	    	for (int count=0;tempStr != null; count++) {
	    		helpText.add(count, tempStr);
	    		tempStr = br.readLine();
	    	}			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void destroy() {}
}
